package spring.ioc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import spring.ioc.controller.HelloController;
import spring.ioc.model.Student;
import spring.ioc.service.UserService;

//@ComponentScan({"spring.ioc.config","spring.ioc.model"})
//可以配置多个路径
//通过ComponentScan可以修改扫描路径，默认路径是当前包以及包里面所有的

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context=SpringApplication.run(DemoApplication.class, args);

		//Controller类例子表示
		//获取对象,getbean传类型(适合该类型的对象只有一个)
//		HelloController bean = context.getBean(HelloController.class);
//		bean.print();
//
////		HelloController controller=new HelloController();
////		controller.print();
//
//		//获取对象，getbean传name
//		//通过getbean获取名称的方法需要用小驼峰,特殊情况就是前面第一个字母和第二个字母以上都是大写，那就用原生的，不用改名字
//		HelloController bean2=(HelloController) context.getBean("helloController");
//		bean2.print();
//
//		//获取对象，getbean传name和对象(优点，不用强转)
//		HelloController bean3 = context.getBean("helloController", HelloController.class);
//		bean3.print();
//
//		//三个打印是一样的，同一个对象多次获取，地址是不会变得
//		System.out.println(bean);
//		System.out.println(bean2);
//		System.out.println(bean3);









//		//五大注解用spring管理例子
//		HelloController bean1=(HelloController) context.getBean("helloController");
//		bean1.print();
//		HController bean5=(HController) context.getBean("abcd");
//		bean5.print();
//		UserService bean=context.getBean(UserService.class);
//		bean.print();
//		UserRepository bean2 = context.getBean(UserRepository.class);
//		bean2.print();
//		UserComponent bean3 = context.getBean(UserComponent.class);
//		bean3.print();
//		UserConfig bean4 = context.getBean(UserConfig.class);
//		bean4.print();







	//bean
//		Student bean1 = context.getBean("s4",Student.class);
//		System.out.println(bean1);
//
//		Student bean2 = context.getBean("s2",Student.class);
//		System.out.println(bean2);
//
//		Student bean3= context.getBean("s5",Student.class);
//		System.out.println(bean3);











		//DI测试
//		HelloController bean1= context.getBean("helloController", HelloController.class);
//		bean1.print();

		UserService bean = context.getBean(UserService.class);
		bean.print();
	}

}
